
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "BC26103.h"
#include "stdio.h"
#include <string.h>
#include "main.h"
#include "TelecomPlatform.h"
extern USART_RECEIVETYPE UsartType1;
 unsigned char UART1_rec_buf[90];
 unsigned int UART1_rec_counter;
extern unsigned char UART1_Rx_Buf[MAX_REC_LENGTH];
extern unsigned int  UART1_Rx_cnt ;
extern unsigned char UART1_temp[REC_LENGTH];
extern uint8_t Operators;
uint8_t NB_NextSocketNumber=0 ;
void BC26_Power()
{
    printf("--->>BC26 power on...\n");
		NB_PW_OFF;
		HAL_Delay(2000);
		NB_PW_ON;
    PSM_EINT3V3_Low;
    BC26_Reset_Low ;
    BC26_POWKEY_High ;
    HAL_Delay(1000);
    BC26_POWKEY_Low ;
    HAL_Delay(100);
    BC26_Reset_High ;
    HAL_Delay(1000);
    BC26_Reset_Low ;
    HAL_Delay(2000);
}
void BC26_ATI_Status(void)
{
    uint8_t BC26_ATI_cmd[5+1]={'A','T','I','\r','\n',0};
    uint8_t i=10;
    printf("\n--->>>BC26_ATI_Status...\r\n");
	while(i--)
	{
		UART1_Rx_cnt = 0;
		SendDataToNBModule(BC26_ATI_cmd,5);
		HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
		UART1_Rx_Buf[UART1_Rx_cnt] = 0;
		printf("%s\n",UART1_Rx_Buf);
		if(strstr(UART1_Rx_Buf,"OK")!=NULL)
		{
			printf("BC26_ATI_Status is OK\r\n");
			return ;
		}
	}
	printf("BC26_ATI_Status is ERR\r\n");
}
void BC26_CFUN(void)
{
	uint8_t BC26_CFUN_cmd[10+1]={'A','T','+','C','F','U','N','?','\r','\n'};
    uint8_t i=5;
    printf("\n--->>>Set UE Functionality...\n");
    while(i--)
    {
        UART1_rec_counter=0 ;
        SendDataToNBModule(BC26_CFUN_cmd,10);
        HAL_Delay(2000);
        printf("len=%d\n",UART1_rec_counter);
        UART1_rec_buf[UART1_rec_counter]=0 ;
        printf("%s\r\n",UART1_rec_buf);
        if((strncmp(UART1_rec_buf,BC26_CFUN_cmd,9)==0)&&\
        (strstr(UART1_rec_buf,"OK")!=NULL))
        {
            printf("BC26_CFUN is OK\n");
            return ;
        }
        else
        {
            printf("BC26_CFUN is ERR\n");
        }
    }
}
uint8_t BC26_Request_ICCID(void)
{
	uint8_t BC26_Check_CIMI_cmd[10+1]={'A','T','+','Q','C','C','I','D','\r','\n'};
    uint8_t i=5;
	 uint8_t j=0;
		uint8_t k=0;
    printf("--->>>Request_ICCID...\r\n");
    while(i--)
    {
        UART1_Rx_cnt = 0;;
        SendDataToNBModule(BC26_Check_CIMI_cmd,9);
        HAL_Delay(1000);
        printf("len=%d\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt] = 0;
				printf("%s\n",UART1_Rx_Buf);
        if(strstr(UART1_Rx_Buf,"OK")!=NULL)
        {
        	if(UART1_Rx_cnt>=30)
					{
							for(j=0;j<UART1_Rx_cnt-24;j++)
								if( (UART1_Rx_Buf[j]=='I') && (UART1_Rx_Buf[j+1]=='D') && (UART1_Rx_Buf[j+2]==':') && (UART1_Rx_Buf[j+24]=='\r') )
										{
											for(k=0;k<20;k++)
												{
														BC26_ICCID[k] = UART1_Rx_Buf[j+4+k];
												}
												printf("ICCID=%s\r\n",BC26_ICCID);
												return	TRUE;
										}
					}
				}
     }
		printf("NO SIM Card\r\n");
    return FALSE ;
}
uint8_t BC26_Request_IMEI(void)
{
	uint8_t BC26_CGSN_cmd[11+1]={'A','T','+','C','G','S','N','=','1','\r','\n'};
    uint8_t i=5;
	uint8_t j=0;
		uint8_t k=0;
    printf("Request IMEI...\r\n");
    while(i--)
    {
        UART1_Rx_cnt = 0;
        SendDataToNBModule(BC26_CGSN_cmd,11);
        HAL_Delay(1000);
        printf("len=%d\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt] = 0;
		printf("%s\n",UART1_Rx_Buf);
        if(strstr(UART1_Rx_Buf,"OK")!=NULL)
        {
			if(UART1_Rx_cnt>=24)
			{
				for(j=0;j<UART1_Rx_cnt-19;j++)
					if( (UART1_Rx_Buf[j]=='S') && (UART1_Rx_Buf[j+1]=='N') && (UART1_Rx_Buf[j+2]==':') && (UART1_Rx_Buf[j+19]=='\r') )
					{
						for(k=0;k<15;k++)
						{
							BC26_IMEI[k] = UART1_Rx_Buf[j+4+k];
						}
					}
			}
			printf("BC26_IMEI=%s\r\n",BC26_IMEI);
			return TRUE ;
		}
		else
		{
			printf("read IMEI error\r\n");
		}
    }
    printf("read IMEI error\n");
    return FALSE ;
}
uint8_t BC26_Active_Net(void)
{
	uint8_t BC26_CGATT_cmd[12+1]={'A','T','+','C','G','A','T','T','=','1','\r','\n'};
    uint8_t i=10;
	uint8_t j=0;
    printf("\n--->>>BC26_Active_Net...\r\n");
	UART1_Rx_cnt=0 ;
    while(i--)
    {
		SendDataToNBModule(BC26_CGATT_cmd,12);
		HAL_Delay(1000);
		HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        printf("%s\r\n",UART1_Rx_Buf);
		if(strstr(UART1_Rx_Buf,"OK")!=NULL)
		{
			printf("BC26_Active_Net is OK\r\n");
			return TRUE;
		}
		else
		{
			HAL_Delay(1000);
			HAL_Delay(1000);
			HAL_Delay(1000);
			HAL_Delay(1000);
			HAL_Delay(1000);
		}
	}
	printf("BC26_Active_Net is ERR\r\n");
	Time3_ch1_on();
	HAL_Delay(100);
	Time3_ch1_off();
	HAL_Delay(100);
	Time3_ch1_on();
	HAL_Delay(100);
	Time3_ch1_off();
	HAL_Delay(100);
	return FALSE ;
}
uint8_t Check_BC26_Net_Status(void)
{
	uint8_t BC26_Check_CGATT_cmd[11+1]={'A','T','+','C','G','A','T','T','?','\r','\n'};
    uint8_t i=5,result=0 ;
    printf("\n--->>>Check_BC26_Net_Status...\r\n");
    while(i--)
    {
        UART1_Rx_cnt=0 ;
        SendDataToNBModule(BC26_Check_CGATT_cmd,11);
        HAL_Delay(1000);
				HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        printf("%s\r\n",UART1_Rx_Buf);
		if(strstr(UART1_Rx_Buf,"OK")!=NULL)
		{
			printf("Check_BC26_Net_Status is OK\r\n");
			return TRUE;
		}
		else
		{
			printf("Check_BC26_Net_Status is ERR\r\n");
		}
	}
	return FALSE ;
}
void BC26_Check_Band(void)
{
	uint8_t BC26_Check_Band_cmd[11+1]={'A','T','+','Q','B','A','N','D','?','\r','\n'};
    uint8_t i=5;
    printf("\n--->>>Query the list of supported bands...\r\n");
    while(i--)
    {
        UART1_Rx_cnt=0 ;
		SendDataToNBModule(BC26_Check_Band_cmd,11);
        HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        printf("%s\r\n",UART1_Rx_Buf);
		if(strstr(UART1_Rx_Buf,"OK")!=NULL)
		{
			printf("BC26_Check_Band is OK\r\n");
			return;
		}
		else
		{
			printf("BC26_Check_Band is ERR\r\n");
		}
    }
	printf("BC26_Check_Band is ERR\r\n");
}
uint8_t BC26_Check_CESQ(void)
{
	uint8_t BC26_Check_CESQ_cmd[9+1]={'A','T','+','C','E','S','Q','\r','\n'};
    uint8_t i=5,j=0,result=0 ;
    printf("--->>>Query the signal strength...\r\n");
    while(i--)
    {
        UART1_Rx_cnt=0 ;
        SendDataToNBModule(BC26_Check_CESQ_cmd,9);
        HAL_Delay(2000);
        printf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        printf("%s\n",UART1_Rx_Buf);
        if(strstr(UART1_Rx_Buf,"OK")!=NULL)
        {
            if(UART1_Rx_cnt>=25)
			{
				for(j=0;j<UART1_Rx_cnt-5;j++)
				{
					if((UART1_Rx_Buf[j]=='Q') && (UART1_Rx_Buf[j+1]==':') && (UART1_Rx_Buf[j+2]==' ') && (UART1_Rx_Buf[j+5]==','))
					{
						result = ((UART1_Rx_Buf[j+3]-0x30)<<3) + ((UART1_Rx_Buf[j+3]-0x30)<<1) + (UART1_Rx_Buf[j+4]-0x30);
					}
					else if((UART1_Rx_Buf[j]=='Q') && (UART1_Rx_Buf[j+1]==':') && (UART1_Rx_Buf[j+2]==' ') && (UART1_Rx_Buf[j+4]==','))
					{
						result = (UART1_Rx_Buf[j+3]-0x30);
					}
				}
			}
            printf("BC26_Check_CESQ is: %d\r\n",result);
            return result ;
        }
        else
        {
            printf("BC26_Check_CESQ is ERR\n");
        }
    }
    return 0 ;
}
int BC26_Register_Net_Status(void)
{
	uint8_t BC26_Register_Net_Status_cmd[11+1]={'A','T','+','C','E','R','E','G','?','\r','\n'};
    uint8_t i=1,j=0,result=0 ;
    printf("\n--->>>��ѯ NB ����ע��״̬\n");
    while(i--)
    {
        UART1_Rx_cnt=0 ;
		SendDataToNBModule(BC26_Register_Net_Status_cmd,11);
        HAL_Delay(1000);
			HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        printf("%s\r\n",UART1_Rx_Buf);
		if(strstr(UART1_Rx_Buf,"OK")!=NULL)
		{
			if(UART1_Rx_cnt>=29)
			{
				for(j=0;j<UART1_Rx_cnt-2;j++)
				{
					if( (UART1_Rx_Buf[j]==',') && (UART1_Rx_Buf[j+2]=='\r'))
					{
						result = UART1_Rx_Buf[j+1] -0x30;
						printf("BC26_Register_Net_Status is OK=%d\r\n",result);
						if(result==1)
						{
							return result ;
						}
					}
				}
			}
			printf("BC26_Register_Net_Status is OK\r\n");
			return TRUE;
		}
		else
		{
			 HAL_Delay(1000);
			 HAL_Delay(1000);
			 HAL_Delay(1000);
			 HAL_Delay(1000);
		}
	}
		printf("BC26_Register_Net_Status is ERR\r\n");
		return FALSE;
}
uint8_t BC26_CSCON(void)
{
	uint8_t BC26_CSCON_cmd[11+1]={'A','T','+','C','S','C','O','N','?','\r','\n'};
    uint8_t i=5,result=0 ;
	uint8_t j=0;
    printf("\n--->>>��ѯ����״̬\r\n");
    while(i--)
    {
		UART1_Rx_cnt=0 ;
		SendDataToNBModule(BC26_CSCON_cmd,11);
        HAL_Delay(1000);
			HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        printf("%s\r\n",UART1_Rx_Buf);
		if(strstr(UART1_Rx_Buf,"OK")!=NULL)
		{
			if(UART1_Rx_cnt>=13)
			{
				for(j=0;j<UART1_Rx_cnt-2;j++)
				{
					if( (UART1_Rx_Buf[j]==',') && ((UART1_Rx_Buf[j+2]=='\r')) )
					{
						if((UART1_Rx_Buf[j+1]=='1'))
						{
							printf("Signalling Connection Status is Connetced\r\n");
						}
						else if((UART1_Rx_Buf[j+1]=='0'))
						{
							HAL_Delay(1000);
							HAL_Delay(1000);
							HAL_Delay(1000);
							HAL_Delay(1000);
						}
					}
				}
			}
			printf("BC26_CSCON is OK\r\n");
			return TRUE;
		}
		else
		{
			printf("BC26_CSCON is ERR\r\n");
		}
	}
		printf("Signalling Connection Status is IDIE\r\n");
    return FALSE ;
}
uint8_t BC26_COPS(void)
{
	uint8_t BC26_COPS_cmd[10+1]={'A','T','+','C','O','P','S','?','\r','\n'};
	uint8_t BC26_COPS[5+1]={0};
    uint8_t i=5 ;
	uint8_t j=0 ;
    uint16_t result=0 ;
    uint8_t startflag[3]={',','"',0};
    uint8_t endflag[3]={'"',',',0};
    printf("\n--->>>��ѯ��Ӫ��\n");
    while(i--)
    {
        UART1_Rx_cnt=0 ;
		SendDataToNBModule(BC26_COPS_cmd,10);
        HAL_Delay(1000);
		HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        printf("%s\r\n",UART1_Rx_Buf);
		if(strstr(UART1_Rx_Buf,"OK")!=NULL)
		{
			if(strstr(UART1_Rx_Buf,"46000")!=NULL)
			{
				printf("�й��ƶ�\r\n");
				return 1;
			}
			else if(strstr(UART1_Rx_Buf,"46001")!=NULL)
			{
				printf("�й���ͨ\r\n");
				return 2;
			}
			else if(strstr(UART1_Rx_Buf,"46011")!=NULL)
			{
				printf("�й�����\r\n");
				return 3;
			}
			else
			{
				 HAL_Delay(1000);
				 HAL_Delay(1000);
				 HAL_Delay(1000);
				 HAL_Delay(1000);
			}
		}
		else
		{
			printf("BC26_COPS is ERR\r\n");
		}
	}
	printf("��ѯ������Ӫ��\r\n");
	return 0;
}
void BC26_PDPACT(void)
{
	uint8_t BC26_CGPADDR_cmd[14+1]={'A','T','+','C','G','P','A','D','D','R','=','1','\r','\n'};
    uint8_t i=5 ;
    printf("\n--->>>Get the IP address of the device...\r\n");
    while(i--)
    {
		UART1_Rx_cnt=0 ;
        SendDataToNBModule(BC26_CGPADDR_cmd,14);
        HAL_Delay(1000);
			 HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        printf("%s\r\n",UART1_Rx_Buf);
		if(strstr(UART1_Rx_Buf,"OK")!=NULL)
		{
			printf("BC26_PDPACT is OK\r\n");
			i=0 ;
			return ;
		}
		else
		{
			HAL_Delay(1000);
			HAL_Delay(1000);
			HAL_Delay(1000);
			HAL_Delay(1000);
		}
    }
		printf("BC26_PDPACT is ERR\r\n");
}
int8_t CreatSocket(uint8_t type,uint8_t socketid,uint8_t*serveriP,uint8_t*serverport,uint8_t*localport)
{
    uint8_t i=5,j=0,m=0,sockedid=0,result=0 ;
    uint8_t ATStrBuf[50]={0};
	uint8_t Buf4[6]={'"','U','D','P','"',0};
    uint8_t Buf[6]={'"','T','C','P','"',0};
    uint8_t Buf1[6]={',','"',0 };
    uint8_t Buf2[6]={'"',',',0};
    uint8_t Buf3[2]={',',0 };
    uint8_t*p=NULL ;
    printf("\n--->>>Connect Socket to Remote Address and Port...\r\n");
		sprintf(ATStrBuf,"AT+QIOPEN=%d,%d,",1,socketid);
    if(type==TCP_Connection)
    {
    	strcat(ATStrBuf,Buf);
    }
	else  if(type==UDP_Connection)
	{
		strcat(ATStrBuf,Buf4);
	}
    else
    {
		printf("socket type err\r\n");
	}
    strcat(ATStrBuf,Buf1);
    strcat(ATStrBuf,serveriP);
    strcat(ATStrBuf,Buf2);
    strcat(ATStrBuf,serverport);
    strcat(ATStrBuf,Buf3);
    strcat(ATStrBuf,localport);
    strcat(ATStrBuf,Buf3);
    strcat(ATStrBuf,"0,");
    strcat(ATStrBuf,"0");
    strcat(ATStrBuf,"\r\n\0");
    printf("ATStrBuf=%s\r\n",ATStrBuf);
    i=1;
    while(i--)
    {
				UART1_Rx_cnt=0 ;
        SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
				HAL_Delay(1000);
				printf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        printf("%s\r\n",UART1_Rx_Buf);
				if( (strstr(UART1_Rx_Buf,"+QIOPEN:")!=NULL) && (strstr(UART1_Rx_Buf,"OK")!=NULL) )
				{
						if(UART1_Rx_cnt>=13)
						{
							for(m=0;m<UART1_Rx_cnt-6;m++)
							{
								if( (UART1_Rx_Buf[m]=='P') && (UART1_Rx_Buf[m+1]=='E') && (UART1_Rx_Buf[m+2]=='N') && (UART1_Rx_Buf[m+3]==':') && (UART1_Rx_Buf[m+4]==' ') && (UART1_Rx_Buf[m+6]==',') )
								{
										sockedid = UART1_Rx_Buf[m+5] - 0x30;
										printf("creak socket %d is OK\r\n",sockedid);
										return sockedid;
								}
							}
						}
				}
			else
			{
				printf("creak socket is ERR\r\n");
			}
    }
    return-1 ;
}
uint8_t SendDataToServer(uint8_t socketid,uint8_t*buffer,uint8_t len)
{
    uint8_t i=5;
    uint8_t ATStrBuf[100]={0};
    sprintf(ATStrBuf,"AT+QISEND=%d,%d,%s\r\n",socketid,len,buffer);
    while(i--)
    {
        UART1_Rx_cnt=0 ;
        SendDataToNBModule(ATStrBuf,strlen(ATStrBuf)+1);
        HAL_Delay(1000);
		HAL_Delay(1000);
			HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        printf("%s\r\n",UART1_Rx_Buf);
        if(strstr(UART1_Rx_Buf,"SEND OK")!=NULL)
        {
            printf("Send Data success\r\n");
            return TRUE ;
        }
        else
        {
            printf("Send Data  ERR\r\n");
        }
    }
    return FALSE ;
}
uint8_t ReceiveDataFromServer(uint8_t socketid,uint8_t*buffer)
{
    uint8_t ATStrBuf[50]={0},i=0,j=0,k=0,len ;
    uint8_t*p1,*p2 ;
    sprintf(ATStrBuf,"AT+QIRD=%d,%3d\r\n",socketid,512);
   for(i=0;i<10;i++)
    {
		len = 0;
		UART1_Rx_cnt=0 ;
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
    HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        printf("%s\r\n",UART1_Rx_Buf);
        if(strstr(UART1_Rx_Buf,"OK")!=NULL)
        {
            if(UART1_Rx_cnt>=10)
			{
				for(k=0;k<UART1_Rx_cnt-10;k++)
				{
					if( (UART1_Rx_Buf[k+0]=='Q') && (UART1_Rx_Buf[k+1]=='I') && (UART1_Rx_Buf[k+2]=='R') && (UART1_Rx_Buf[k+3]=='D') && (UART1_Rx_Buf[k+4]==':') && (UART1_Rx_Buf[k+5]==' ') && (UART1_Rx_Buf[k+8]=='\r')  )
					{
						len = ((UART1_Rx_Buf[k+6]-'0')<<4) + (UART1_Rx_Buf[k+7]-'0');
						i=10;
						break;
					}
				}
			}
		}
	}
    printf("receive Data len=%d\r\n",len);
    if((len>0)&&(len<=ReceiveUdpDataMaxLen))
    {
        p1=strstr(UART1_Rx_Buf,"+QIRD:");
		p2=strstr(p1,"\r\n");
        memcpy(buffer,p2+2,len);
		buffer[len]=0;
		for(j=IMEI_Len+2-1;j<len;j++)
		{
			if(buffer[j]>='A'&&buffer[j]<='Z')
			{
				buffer[j]=buffer[j]+32;
			}
		}
        printf("receive Data=%s\r\n",buffer);
		return len ;
    }
	else
	{
		printf("Receive Data ERR\r\n");
		return	0;
	}
    return 0 ;
}
uint8_t	BC26_ConfigSleepMode(uint8_t		mode)
{
	uint8_t BC26_QSCLK0_cmd[14+1]={'A','T','+','Q','S','C','L','K','=','0','\r','\n'};
	uint8_t BC26_QSCLK1_cmd[14+1]={'A','T','+','Q','S','C','L','K','=','1','\r','\n'};
	uint8_t	buffer[20]={0};
	uint8_t i=5,result=0 ;
	if(mode==BC26_DisableSleepMode)
	{
		memcpy(buffer,BC26_QSCLK0_cmd,12);
	}
	else if(mode==BC26_EnableSleepMode)
	{
		memcpy(buffer,BC26_QSCLK1_cmd,12);
	}
	else
	{
		printf("BC26_ConfigSleepMode err\n");
		return FALSE;
	}
	printf("\n--->>>�������� %d\r\n",mode);
	while(i--)
	{
		UART1_Rx_cnt=0 ;
 		SendDataToNBModule(buffer,13);
		HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        printf("%s\r\n",UART1_Rx_Buf);
		if(strstr(UART1_Rx_Buf,"OK")!=NULL)
		{
			printf("BC26_ConfigSleepMode is OK\r\n");
			return TRUE;
		}
		else
		{
			printf("BBC26_ConfigSleepMode is ERR\r\n");
		}
	}
	printf("BC26_ConfigSleepMode is ERR\r\n");
	return FALSE ;
}
uint8_t CloseSocket(uint8_t socketid)
{
    uint8_t i=3 ;
    uint8_t ATStrBuf[50]={0};
    printf("\n--->>>Close Socket...\r\n");
    sprintf(ATStrBuf,"AT+QICLOSE=%d\r\n",socketid);
    while(i--)
    {
		UART1_Rx_cnt=0 ;
		SendDataToNBModule(ATStrBuf,strlen(ATStrBuf));
        HAL_Delay(1000);
		HAL_Delay(1000);
		printf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        printf("%s\r\n",UART1_Rx_Buf);
        if(strstr(UART1_Rx_Buf,"OK")!=NULL)
        {
            printf("Close Socket success\r\n");
            return TRUE ;
        }
        else
        {
            printf("Close Socket ERR\r\n");
        }
    }
    return FALSE ;
}
uint8_t BC26_QCSEARFCN(void)
{
	uint8_t BC26_CGPADDR_cmd[14+1]={'A','T','+','Q','C','S','E','A','R','F','C','N','\r','\n'};
    uint8_t i=3,result;
	uint8_t k=0;
    printf("\n--->>>Clear NB-IoT Stored EARFCN List\r\n");
    while(i--)
    {
        UART1_Rx_cnt=0 ;
        SendDataToNBModule(BC26_CGPADDR_cmd,14);
        HAL_Delay(1000);
        printf("len=%d\r\n",UART1_Rx_cnt);
        UART1_Rx_Buf[UART1_Rx_cnt]=0 ;
        printf("%s\r\n",UART1_Rx_Buf);
        if(strstr(UART1_Rx_Buf,"OK")!=NULL)
        {
			if(UART1_Rx_cnt>=15)
			{
				for(k=0;k<UART1_Rx_cnt-8;k++)
				{
					if( (UART1_Rx_Buf[k+0]=='A') && (UART1_Rx_Buf[k+1]=='R') && (UART1_Rx_Buf[k+2]=='F') && (UART1_Rx_Buf[k+3]=='C') && (UART1_Rx_Buf[k+4]=='N') && (UART1_Rx_Buf[k+5]==':') && (UART1_Rx_Buf[k+6]==' ') && (UART1_Rx_Buf[k+8]=='\r')  )
					{
						result = UART1_Rx_Buf[k+7]-'0';
						if((result==0)||(result==3))
						{
							printf("AT+QCSEARFCN OK\r\n");
							return TRUE;
						}
					}
				}
			}
        }
        else
        {
        	HAL_Delay(1000);
			HAL_Delay(1000);
			HAL_Delay(1000);
			HAL_Delay(1000);
        }
    }
	printf("AT+QCSEARFCNT is ERR\n");
    return FALSE;
}
void BC26_PowerStart(void)
{
    BC26_Power();
    BC26_ATI_Status();
    BC26_ConfigSleepMode(BC26_DisableSleepMode);
}
int8_t BC26_IntialAndCreatSocket(void)
{
	int8_t	socketid=-1;
	uint8_t time=0;
	uint8_t SignalStrength = 0;
	if(BC26_QCSEARFCN()==FALSE)
	{
		BC26_Power();
		BC26_ATI_Status();
		BC26_QCSEARFCN();
	}
    BC26_Power();
    BC26_ATI_Status();
    BC26_ConfigSleepMode(BC26_DisableSleepMode);
	if(BC26_Request_ICCID()==FALSE)
	{
		return	-1;
	}
	BC26_Request_IMEI();
	while(1)
    {
    	if(++time>5)
    	{
			break;
		}
    	SignalStrength=BC26_Check_CESQ();
		if(SignalStrength<10)
		{
			HAL_Delay(5000);
		}
		else
		{
			break;
		}
    }
	if(BC26_Active_Net()==FALSE)
    {
		return	-1;
	}
	 time=0;
	if(BC26_Register_Net_Status()==1)
	{
		BC26_CSCON();
		Operators = BC26_COPS();
		BC26_PDPACT();
		if(Operators==1)
		{
			BC26_ConfigSleepMode(BC26_DisableSleepMode);
			while(socketid<0)
			{
				if(++time>3)
				{
					printf("CreatSocket err\r\n");
					return	-1;
				}
				socketid=CreatSocket(UDP_Connection,NB_NextSocketNumber,SeverIpAddress,UDPServerPort,NBLocalPort);
				if(socketid<0)
				{
					CloseSocket(NB_NextSocketNumber);
				}
				++NB_NextSocketNumber ;
				if(NB_NextSocketNumber==3)
				{
					NB_NextSocketNumber=0 ;
				}
			}
			return	socketid;
		}
		else if(Operators==2)
		{
		}
		else if(Operators==3)
		{
			if(BC26_QLWSERV(TelecomPlatform_IP,TelecomPlatform_port)==TRUE)
			{
				BC26_QLWCONF(BC26_IMEI);
				BC26_QLWADDOBJ();
				if(BC26_QLWOPEN()==TRUE)
				{
					BC26_QLWCFG();
					BC26_QLWUPDATE();
					return	0;
				}
			}
		}
		else
		{
			return -1;
		}
	}
}
void	SendDataToNBModule(uint8_t	*buf,uint8_t	len)
{
	HAL_UART_Transmit(&huart1,buf,len,100);
}